package com.zzt.zt_scaleparentviewgroup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String value = "fdaflkjl    www.baidu.com   jfkdajfd  skjfldsa f  fdka  http://www.xtrendspeed.com jkfldjakl fjdlafkajf  https://www.qq.com fdaklfj https://kaifa.baidu.com/searchPage?wd=java&module=SUG fdalje eiowu hioeweo ijfkdsl ";
        String regexHttp2 = "(((https|http)?://)?" +
                "[\\w\\-_]+" +
                "(\\.[\\w\\-_]+)+" +
                "([\\w\\-.,@?^=%&:/~+#]*[\\w\\-@?^=%&/~+#])?)";
        Pattern patternHttp2 = Pattern.compile(regexHttp2);
        Matcher matcherHttp2 = patternHttp2.matcher(value);
        while (matcherHttp2.find()) {
            int start = matcherHttp2.start();
            int end = matcherHttp2.end();
            String atagString = value.substring(start, end);
            Log.d(TAG, "html 解析，222 查找内容：" + atagString);
        }


        double ddddd = 157.93052D;
        String s = formatByPointPow(ddddd, 2);
        Log.d(TAG, "0000000:::::" + s);
    }


    public String formatByPointPow(double d, int pow) {
        int length = getPointPow(d);
        if (length > pow)
            pow = length;
        String formatStr = "##0";
        if (pow > 0) {
            formatStr += ".";
        }
        for (int i = 0; i < pow; i++)
            formatStr += "0";
        DecimalFormat format = new DecimalFormat(formatStr, getDecimalFormatSymbols());
        String format1 = format.format(d);

        Log.i("Log.TAG", "=====bd1=" + format1);

        return format1;
    }

    public static DecimalFormatSymbols getDecimalFormatSymbols() {
        return new DecimalFormatSymbols(new Locale("en", "US"));
    }

    public int getPointPow(double d) {
        try {
            String str = d + "";
            //去掉无效0
            str = moveLast0(str);
            if (str.contains(".")) {
                String[] ss = str.split("\\.");
                return ss[1].length();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String moveLast0(String str) {
        try {
            if (str == null)
                return "";
            BigDecimal bigDecimal = new BigDecimal(str);
            str = bigDecimal.toString();
            if (str.contains(".")) {
                String[] ss = str.split("\\.");
                if (Integer.parseInt(ss[1]) > 0) {
                    if (ss[1].endsWith("0")) {
                        int index = ss[1].lastIndexOf("0");
                        str = ss[0] + "." + ss[1].substring(0, index);
                        if (str.endsWith("0")) {
                            return moveLast0(str);
                        }
                    } else {
                        str = ss[0] + "." + ss[1];
                    }
                } else {
                    str = ss[0];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
}